package apiserver

import (
	"gitee.com/youkelike/ziam/zapp/app"
	"gitee.com/youkelike/ziam/zserver/apiserver/options"
	"gitee.com/youkelike/zlog"
)

const commandDesc = `The ZIAM API server validates and configures data
for the api objects which include users, policies, secrets, and
others. The API Server services REST operations to do the api objects management.`

func NewApp(basename string) *app.App {
	// 这里的 options 是每个应用特有的，
	// 在 zapp 中，启动 cmd 前，会调用它的 Flags() 方法，把它的指定字段添加到命令行选项中
	opts := options.NewOptions()
	application := app.NewApp("ZIAM API SERVER",
		basename,
		app.WithOptions(opts),
		app.WithDescription(commandDesc),
		app.WithDefaultValidArgs(),
		app.WithRunFunc(run(opts)),
	)

	return application
}

func run(opts *options.Options) app.RunFunc {
	return func(basename string) error {
		zlog.Init(opts.Log)
		defer zlog.Sync()

		cfg, err := CreateConfigFromOptions(opts)
		if err != nil {
			return err
		}

		return Run(cfg)
	}
}
